/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2024/6/28
 * Time: 11:40
 */

#include "HandCardArea.h"

#include "CardUi.h"
#include "OverlapLayout.h"

namespace Upgrade {

    HandCardArea::HandCardArea(QWidget *_parent /* = nullptr*/)
        : QWidget(_parent)
        , select_offset_(50)
        , main_layout_(new OverlapLayout(this, select_offset_))
    {
        auto margin = main_layout_->contentsMargins();
        margin.setTop(margin.top() + select_offset_ + 5);
        main_layout_->setContentsMargins(margin);
    }

    void HandCardArea::SetReorderSuit(const QList<CardSuit> &_order,CardSuit _currentSuit)
    {
        suit_order_ = _order;
        main_layout_->SetSortFunction<Card>([=](Card A, Card B) -> bool {
            auto a_suit_index = suit_order_.indexOf(A.suit);
            auto b_suit_index = suit_order_.indexOf(B.suit);
            return tie(A.trump, a_suit_index, A.rank) > tie(B.trump, b_suit_index, B.rank);
        });

        for (auto card : all_card_) {
            auto suit = card->suit;
            if(suit == _currentSuit)
                card->SetTrump(true);
        }

        main_layout_->Resort();
    }

    void HandCardArea::AddWidget(const CardSPtr &_card)
    {
        auto card_ui = new CardUi(_card, this);
        card_ui->setProperty(main_layout_->GetSortPropertyName().toLocal8Bit(), QVariant::fromValue(*card_ui->Card()));
        all_card_.append(_card);

        main_layout_->addWidget(card_ui);
    }

    void HandCardArea::DealSelect(CardUi *_card)
    {
        auto select = _card->property("SelectProperty").toBool();
        auto start  = _card->geometry();
        auto end    = start;
        if(select)
            end.moveTop(end.top() + select_offset_);
        else
            end.moveTop(end.top() - select_offset_);

        _card->setProperty("SelectProperty", !select);

        auto animation = new QPropertyAnimation(_card, "geometry");
        animation->setDuration(100);
        animation->setStartValue(start);
        animation->setEndValue(end);
        animation->start(QAbstractAnimation::DeleteWhenStopped);
    }

    void HandCardArea::mousePressEvent(QMouseEvent *event)
    {
        if(event->button() != Qt::LeftButton)
            return;

        is_dragging = false;
        drag_start  = event->pos();
        QWidget::mousePressEvent(event);
    }

    void HandCardArea::mouseReleaseEvent(QMouseEvent *event)
    {
        if(event->button() != Qt::LeftButton)
            return;

        // 获取鼠标点击或拖动时的起始和结束。点击时起始=结束
        if(is_dragging) {
            drag_end = event->pos();
            if(drag_start.x() > drag_end.x())
                qSwap(drag_start, drag_end);
        }
        else
            drag_start = drag_end = event->pos();

        // 构造选择矩形。点击是矩形size = {1,1}
        auto select_rect = QRectF(drag_start, drag_end);
        if(select_rect.size().isNull())
            select_rect.setSize({1, 1});

        // 统一点击和滑动选择，遍历每个item
        for(int i = 0; i < main_layout_->count(); ++i) {
            auto widget = main_layout_->itemAt(i)->widget();
            auto pos    = widget->pos();
            // 相对坐标rect = {0,0,150*200}
            auto rect   = widget->visibleRegion().boundingRect();

            // 更改为相对于父窗口的绝对坐标，并调整可视矩形(被其他牌覆盖的部分不计算在内)
            rect.moveTopLeft(pos);
            if(i + 1 != main_layout_->count())
                rect.setWidth(main_layout_->spacing());

            if(select_rect.intersects(rect))
                DealSelect(static_cast<CardUi *>(widget));
        }

        drag_start  = QPointF();
        drag_end    = QPointF();
        is_dragging = false;

        QWidget::mouseReleaseEvent(event);
    }

    void HandCardArea::mouseMoveEvent(QMouseEvent *event)
    {
        if(event->buttons() & Qt::LeftButton) {
            if((event->pos() - drag_start).manhattanLength() > 3)
                is_dragging = true;
        }

        QWidget::mouseMoveEvent(event);
    }

}// namespace Upgrade